package com.lvmama.pangolin.task;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.lvmama.pangolin.common.StringTools;
import com.lvmama.pangolin.entity.CommodityConditionVO;
import com.lvmama.pangolin.entity.RelevanceProduct;
import com.lvmama.pangolin.entity.link.TicketCommodityLink;
import com.lvmama.pangolin.entity.lmm.LMMCareProduct;
import com.lvmama.pangolin.entity.lmm.LMMTicketCommodity;
import com.lvmama.pangolin.entity.match.MatchTicketCommodity;
import com.lvmama.pangolin.entity.qnr.QNRTicketCommodityItem;
import com.lvmama.pangolin.entity.tc.TCommodityItem;
import com.lvmama.pangolin.entity.tn.TNTicketCommodityItem;
import com.lvmama.pangolin.entity.xc.XCTicketCommodityItem;
import com.lvmama.pangolin.lmm.service.LMMCareProductService;
import com.lvmama.pangolin.lmm.service.LMMTicketCommodityService;
import com.lvmama.pangolin.qnr.service.QNRTicketCommodityItemsService;
import com.lvmama.pangolin.service.RelevanceCommodityService;
import com.lvmama.pangolin.service.RelevanceProductService;
import com.lvmama.pangolin.tc.service.TCCommodityItemsService;
import com.lvmama.pangolin.timer.CommonJob;
import com.lvmama.pangolin.tn.service.TNTicketCommodityItemsService;
import com.lvmama.pangolin.util.CommodityMatchUtil;
import com.lvmama.pangolin.util.SpringContextUtil;
import com.lvmama.pangolin.xc.service.XCTicketCommodityItemsService;


public class MatchCommodityData  extends CommonJob
{

	@Autowired
	private LMMCareProductService careProductService;
	
	@Autowired
	private  QNRTicketCommodityItemsService qnrTicketService;
	@Autowired
	private  TCCommodityItemsService tcTicketService;
	@Autowired
	private  XCTicketCommodityItemsService xcTicketService;
	@Autowired
	private  TNTicketCommodityItemsService tnTicketService;
	
	@Autowired
	private LMMTicketCommodityService lmmTicketService;
	@Autowired
	private RelevanceProductService relevanceProductService;
	@Autowired
	private RelevanceCommodityService relevanceCommodityService;
	@Autowired
    private CommodityMatchUtil commodityMatchUtil;
	
	private Logger logger = Logger.getLogger(MatchCommodityData.class);
	
	@Override
	public void runJob(JobExecutionContext context, String parameter)
	{
		logger.info("商品自动匹配开始....");
		//开始匹配同程数据
		if(careProductService == null)
		{
			careProductService = (LMMCareProductService) SpringContextUtil
					.getBean(LMMCareProductService.class);
		}
		if(qnrTicketService == null)
		{
			qnrTicketService = (QNRTicketCommodityItemsService) SpringContextUtil
					.getBean(QNRTicketCommodityItemsService.class);
		}
		if(tcTicketService == null)
		{
			tcTicketService = (TCCommodityItemsService) SpringContextUtil
					.getBean(TCCommodityItemsService.class);
		}
		if(xcTicketService == null)
		{
			xcTicketService = (XCTicketCommodityItemsService) SpringContextUtil
					.getBean(XCTicketCommodityItemsService.class);
		}
		if(tnTicketService == null)
		{
			tnTicketService = (TNTicketCommodityItemsService) SpringContextUtil
					.getBean(TNTicketCommodityItemsService.class);
		}
		if(lmmTicketService == null)
		{
			lmmTicketService = (LMMTicketCommodityService) SpringContextUtil
					.getBean(LMMTicketCommodityService.class);
		}
		if(relevanceProductService == null)
		{
			relevanceProductService = (RelevanceProductService) SpringContextUtil
					.getBean(RelevanceProductService.class);
		}
		if(relevanceCommodityService == null)
		{
			relevanceCommodityService = (RelevanceCommodityService) SpringContextUtil
					.getBean(RelevanceCommodityService.class);
		}
		if(commodityMatchUtil == null)
		{
			commodityMatchUtil = (CommodityMatchUtil) SpringContextUtil
					.getBean(CommodityMatchUtil.class);
		}
		
		
		LMMCareProduct record = new LMMCareProduct();
		record.setBusinessCode("TICKET");
		//获取处理标识为0的所有产品信息
		List<LMMCareProduct> prodList = careProductService.selectByCond(record);
		if(prodList != null && !prodList.isEmpty())
		{
			List<MatchTicketCommodity> lmmmatchList = null;
			MatchTicketCommodity matchCommodity = null;
			String prodName = null;
			String cityCode = null;
			for (LMMCareProduct lmmCareProduct : prodList)
			{
				String prodId = lmmCareProduct.getProductId();
				prodName = lmmCareProduct.getProductName();
				cityCode = lmmCareProduct.getCityName();
				CommodityConditionVO cond = new CommodityConditionVO();
				cond.setProductId(prodId);
				List<LMMTicketCommodity> list = lmmTicketService.selectByProdId(cond);
				if(list != null && !list.isEmpty())
				{
					lmmmatchList = exchangePro(list,prodName,cityCode);
					
					RelevanceProduct relPro = relevanceProductService.viewRelevance(prodId,null);
					if(relPro == null)
					{
						continue;
					}
					//处理携程数据
					List<XCTicketCommodityItem>  xcList = xcTicketService.selectByProdId(relPro.getProdIdXC());
					if(xcList != null && !xcList.isEmpty())
					{
						for (XCTicketCommodityItem item : xcList)
						{
							if(item.getIs_link() == 0)
							{
								matchCommodity = exchangePro(item,prodName,cityCode);
								String id = commodityMatchUtil.maxScore(matchCommodity, lmmmatchList);
								if(StringTools.isValid(id))
								{
									if(dealLink(prodId,id,"xc",item.getProduct_id()
											,item.getCommodity_id(),item.getCommodity_name()))
									{
										xcTicketService.updateByProdCommId(item.getProduct_id(),item.getCommodity_id());
									}
								}
							}
							
						}
					}
					
					//处理途牛数据
					List<TNTicketCommodityItem>  tnList = tnTicketService.selectByProdId(relPro.getProdIdTN());
					if(tnList != null && !tnList.isEmpty())
					{
						for (TNTicketCommodityItem item : tnList)
						{
							if(item.getIs_link() == 0)
							{
								matchCommodity = exchangePro(item,prodName,cityCode);
								String id = commodityMatchUtil.maxScore(matchCommodity, lmmmatchList);
								if(StringTools.isValid(id))
								{
									if(dealLink(prodId,id,"tn",item.getProduct_id()
											,item.getCommodity_id(),item.getCommodity_name()))
									{
										tnTicketService.updateByProdCommId(item.getProduct_id(),item.getCommodity_id());
									}
								}
							}
							
						}
					}
					
					//处理同程数据
					List<TCommodityItem>  tcList = tcTicketService.selectByProdId(relPro.getProdIdTC());
					if(tcList != null && !tcList.isEmpty())
					{
						for (TCommodityItem item : tcList)
						{
							if(item.getIs_link() == 0)
							{
								matchCommodity = exchangePro(item,prodName,cityCode);
								String id = commodityMatchUtil.maxScore(matchCommodity, lmmmatchList);
								if(StringTools.isValid(id))
								{
									if(dealLink(prodId,id,"tc",item.getProduct_id()
											,item.getCommodity_id(),item.getCommodity_name()))
									{
										tcTicketService.updateByProdCommId(item.getProduct_id(),item.getCommodity_id());
									}
								}
							}
							
						}
					}
					
					
					//处理同程数据
					List<QNRTicketCommodityItem>  qnrList = qnrTicketService.selectByProdId(relPro.getProdIdQNR());
					if(qnrList != null && !qnrList.isEmpty())
					{
						for (QNRTicketCommodityItem item : qnrList)
						{
							if(item.getIs_link() == 0)
							{
								matchCommodity = exchangePro(item,prodName,cityCode);
								String id = commodityMatchUtil.maxScore(matchCommodity, lmmmatchList);
								if(StringTools.isValid(id))
								{
									if(dealLink(prodId,id,"qnr",item.getProduct_id()
											,item.getCommodity_id(),item.getCommodity_name()))
									{
										qnrTicketService.updateByProdCommId(item.getProduct_id(),item.getCommodity_id());
									}
								}
							}
							
						}
					}
					
				}
			}
		}
		logger.info("商品自动匹配结束....");
		
	}
	
	private boolean dealLink(String productId,String commodityId,String companyCode,String relprodId,String relcommodityId,String relName)
	{
		boolean insert = false;
		try
		{
			List<TicketCommodityLink> linkL = relevanceCommodityService.selectByCommProdComp(productId,commodityId,companyCode);
			if(linkL == null || linkL.isEmpty())
			{
				TicketCommodityLink link = new TicketCommodityLink();
				link.setCommodity_id(commodityId);
				link.setCompany_code(companyCode);
				link.setProd_id(productId);
				link.setRel_commodity_id(relcommodityId);
				link.setRel_prod_id(relprodId);
				link.setRel_commodity_name(relName);
				relevanceCommodityService.insertCommodityLink(link);
				insert = true;
			}	
		}catch(Exception e)
		{
			logger.error("生成关联关系失败！",e);
		}
		
		return insert;
	}
	
	private List<MatchTicketCommodity> exchangePro(List<LMMTicketCommodity> list,String prodName,String cityCode)
	{
		List<MatchTicketCommodity> matchList = new ArrayList<MatchTicketCommodity>();
		
		if(list != null && !list.isEmpty())
		{
			MatchTicketCommodity matchC = null;
			for (LMMTicketCommodity commodity : list)
			{
				matchC = exchangePro(commodity,prodName,cityCode);
				matchList.add(matchC);
			}
		}
		
		return matchList;
	}
	
	private MatchTicketCommodity exchangePro(LMMTicketCommodity commodity,String prodName,String cityCode)
	{
		MatchTicketCommodity matchC = new MatchTicketCommodity();
		matchC.setCommodityId(commodity.getCommodityId());
		matchC.setCommodityName(commodity.getCommodityName());
		matchC.setPrice(commodity.getSalePrice());
		matchC.setProductName(prodName);
		matchC.setCityCode(cityCode);
		matchC.setProvinceCode(cityCode);
		return matchC;
	}
	
	private MatchTicketCommodity exchangePro(XCTicketCommodityItem commodity,String prodName,String cityCode)
	{
		MatchTicketCommodity matchC = new MatchTicketCommodity();
		matchC.setCommodityId(commodity.getCommodity_id());
		matchC.setCommodityName(commodity.getCommodity_name());
		matchC.setPrice(commodity.getSale_price());
		matchC.setProductName(prodName);
		matchC.setCityCode(cityCode);
		matchC.setProvinceCode(cityCode);
		return matchC;
	}
	private MatchTicketCommodity exchangePro(TNTicketCommodityItem commodity,String prodName,String cityCode)
	{
		MatchTicketCommodity matchC = new MatchTicketCommodity();
		matchC.setCommodityId(commodity.getCommodity_id());
		matchC.setCommodityName(commodity.getCommodity_name());
		matchC.setPrice(commodity.getSale_price());
		matchC.setProductName(prodName);
		matchC.setCityCode(cityCode);
		matchC.setProvinceCode(cityCode);
		return matchC;
	}
	private MatchTicketCommodity exchangePro(QNRTicketCommodityItem commodity,String prodName,String cityCode)
	{
		MatchTicketCommodity matchC = new MatchTicketCommodity();
		matchC.setCommodityId(commodity.getCommodity_id());
		matchC.setCommodityName(commodity.getCommodity_name());
		matchC.setPrice(commodity.getSale_price());
		matchC.setProductName(prodName);
		matchC.setCityCode(cityCode);
		matchC.setProvinceCode(cityCode);
		return matchC;
	}
	private MatchTicketCommodity exchangePro(TCommodityItem commodity,String prodName,String cityCode)
	{
		MatchTicketCommodity matchC = new MatchTicketCommodity();
		matchC.setCommodityId(commodity.getCommodity_id());
		matchC.setCommodityName(commodity.getCommodity_name());
		matchC.setPrice(commodity.getSale_price());
		matchC.setProductName(prodName);
		matchC.setCityCode(cityCode);
		matchC.setProvinceCode(cityCode);
		return matchC;
	}
		
		
	
	
}
